package com.jsonal.common;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.jsonal.config.OSSConfig;
import java.io.File;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Descruption 阿里云oss 上传工具类  高依赖版
 * @ClassName OSSBootUtil
 * @Date 2020/2/1015:12
 */
public class OSSBootUtil {

    private OSSBootUtil() {
    }


    /**
     * oss 工具客户端
     */
    private volatile static OSSClient ossClient = null;

    /**
     * 上传文件至阿里云 OSS 文件上传成功,返回文件完整访问路径 文件上传失败,返回 null
     *
     * @param ossConfig oss 配置信息
     * @param file 待上传文件
     * @param fileUrl 文件保存路径
     * @return oss 中的相对文件路径
     */
    public static String upload(OSSConfig ossConfig, MultipartFile file, String fileUrl) {
        initOSS(ossConfig);
        StringBuilder fileUrlBulider = new StringBuilder(fileUrl);
        try {
            //bucket名   url路径  文件
            ossClient.putObject(ossConfig.getBucketName(), fileUrlBulider.toString(),
                file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        fileUrlBulider = fileUrlBulider.insert(0, ossConfig.getUrl());
        return fileUrlBulider.toString();
    }

    /**
     * 删除 oss 图片 暂时没用
     * @param ossConfig
     * @param fileUrl
     */
    public static void delete(OSSConfig ossConfig, String fileUrl) {
        OSSClient client = initOSS(ossConfig);
        try {
            client.deleteObject(ossConfig.getBucketName(), fileUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化 oss 客户端
     */
    private static OSSClient initOSS(OSSConfig ossConfig) {
        if (ossClient == null) {
            synchronized (OSSBootUtil.class) {
                if (ossClient == null) {
                    ossClient = new OSSClient(ossConfig.getEndpoint(),
                        new DefaultCredentialProvider(ossConfig.getAccesssKeyId(),
                            ossConfig.getAccessKeySecret()),
                        new ClientConfiguration());
                }
            }
        }
        return ossClient;
    }

    public static String uploadImages(OSSConfig ossConfig, MultipartFile file, String fileUrl) {
        String indexOf = ".";
        initOSS(ossConfig);
        StringBuilder fileUrlBulider = new StringBuilder(fileUrl);
        try {
//            createPic(file.getOriginalFilename(), CommonUtil.getGUID(), "ueditor", ossConfig.getUrl(), indexOf);
            //bucket名   url路径  文件
            ossClient.putObject(ossConfig.getBucketName(), fileUrlBulider.toString(),
                file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        fileUrlBulider = fileUrlBulider.insert(0, ossConfig.getUrl());
        return fileUrlBulider.toString();
    }

    private static void createPic(String fileName, String fileId, String folderPath,
        String saveImageLocalPath, String indexOf) throws IOException {
        if (isPic(fileName.substring(fileName.lastIndexOf(indexOf) + 1))) {

            // 生成大图
            String localPath =
                folderPath + "/" + FileUrl.bigImageFolderName.getStr() + "/" + fileId + "/"
                    + File.separator + fileName;

            FileUtil.createThumbnailImage(saveImageLocalPath, localPath,
                FileUrl.bigImageSize.getValue());
            // 生成中图
            localPath =
                folderPath + "/" + FileUrl.middleImageFolderName.getStr() + "/" + fileId + "/"
                    + File.separator + fileName;
            FileUtil.createThumbnailImage(saveImageLocalPath, localPath,
                FileUrl.middleImageSize.getValue());

            // 生成小图
            localPath =
                folderPath + "/" + FileUrl.smallImageFolderName.getStr() + "/" + fileId + "/"
                    + File.separator + fileName;
            FileUtil.createThumbnailImage(saveImageLocalPath, localPath,
                FileUrl.smallImageSize.getValue());
        }
    }

    /**
     * 校验一个文件是否是图片文件
     *
     * @param extension extension: 扩展名
     * @return 如果是图片文件，则返回true，如果不是图片文件则返回false
     */
    @SuppressWarnings("all")
    public static boolean isPic(String extension) {

        try {

            String ext = extension.toLowerCase();
            return "bmp".equals(ext) || "jpg".equals(ext) || "jpeg".equals(ext) || "png".equals(ext)
                || "gif".equals(ext);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
